<template>
  <div class="common-layout">
    <el-container class="layout-container">
      <el-aside width="200px">
        <div class="logo">
          <h3>行情监控系统</h3>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/home/student">
            <el-icon><Money /></el-icon>
            <span>币种列表</span>
          </el-menu-item>
          <el-menu-item index="/home/warning">
            <el-icon><Warning /></el-icon>
            <span>预警统计</span>
          </el-menu-item>
          <el-menu-item index="/home/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container class="main-container">
        <el-header>
          <div class="header-right">
            <span>欢迎，{{ username }}</span>
            <el-button type="text" @click="logout">退出登录</el-button>
          </div>
        </el-header>
        <el-main class="content-main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Warning, Setting, Money } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const username = ref('管理员')

onMounted(() => {
  const storedUsername = localStorage.getItem('username')
  if (storedUsername) {
    username.value = storedUsername
  }
})

const activeMenu = computed(() => {
  return route.path
})

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  router.push('/login')
}
</script>

<style scoped>
.common-layout {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.layout-container {
  height: 100%;
  width: 100%;
}

.el-aside {
  background-color: #304156;
  color: #fff;
  height: 100vh;
  overflow-y: auto;
}

.main-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.logo {
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
}

.el-header {
  background-color: #fff;
  color: #333;
  line-height: 60px;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
}

.content-main {
  flex: 1;
  overflow: auto;
  padding: 0;
  height: calc(100vh - 60px);
}

.header-right {
  float: right;
  display: flex;
  align-items: center;
  gap: 15px;
}

.el-menu-vertical {
  border-right: none;
}
</style>